package com.artconcurrency.chapter4;

import java.util.Date;

/**
 * @ClassName com.artconcurrency.chapter4
 * Description: <类功能描述>. <br>
 * <p>
 * 超时模式
 * </p>
 * @Author tree
 * @Date 2019-05-04 15:45
 * @Version 1.0
 */
public class WaitNotifyTest2 {
    private static Object lock = new Object();

    public static void main(String[] args) {
        System.out.println(getResouce(2000L));
    }

    public static int getResouce(long timeout) {
        synchronized(lock) {
            long future = System.currentTimeMillis() + timeout;
            long remaining = timeout;
            while(remaining > 0) {
                System.out.println("----------> remaining="+remaining);
                try {
                    lock.wait(remaining);
                    remaining = future - System.currentTimeMillis();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    return -1;
                }
            }
            return 1;
        }
    }
}
